<?php
/*
 * @Author: lokei
 * @Date: 2022-11-05 21:47:02
 * @LastEditors: lokei
 * @LastEditTime: 2022-11-09 21:00:43
 * @Description: 
 */
namespace App\Http\Controllers\Master;

use App\Common\Tools\ResultTool;
use App\Http\Controllers\Controller;
use App\Models\Master\MasterModel;
use App\Models\Schedule\Time\ScheduleTimeModel;
use App\Models\Store\City\AreaModel;
use App\Models\Store\StoreModel;
use App\Models\Ucenter\UserModel;
use Illuminate\Http\Request;

class MasterController extends Controller {
    public function get(Request $request) {
        $master = MasterModel::where('id', '=', $request->input('id'))->first();
        $res = ResultTool::success();
        $res['data'] = $master;
        return $res;
    }
    
    public function list(Request $request) {
        $conditions = [];
        if(!is_null($request->input('app_mode')) && $request->input('app_mode') != '') {
            $conditions['app_mode'] = $request->input('app_mode');
        } else if(!is_null($request->header('App-Mode'))) {
            $conditions['app_mode'] = $request->header('App-Mode');
        }
        if(!is_null($request->input('area_id')) && $request->input('area_id') != '') {
            $conditions['area_id'] = $request->input('area_id');
        }
        $items = MasterModel::where($conditions)->get();
        $new_items = [];
        if(!is_null($request->input('day')) && $request->input('day') != '' && !is_null($request->input('slot_id')) && $request->input('slot_id') != '') {
            foreach($items as $item) {
                $schedule_list = ScheduleTimeModel::where('uid', '=', $item->uid)->where('day', '=', $request->input('day'))->where('slot_id', '=', $request->input('slot_id'))->get();
                if(!(count($schedule_list) > 0)) {
                    array_push($new_items, $item);
                }
            }
        } else {
            $new_items = $items;
        }
        $res = ResultTool::success();
        $res['data']['items'] = $new_items;
        $res['data']['total'] = count($new_items);
        return $res;
    }

    public function create(Request $request) {
        $master = new MasterModel();
        if(!is_null($request->header('App-Mode'))) {
            $master->app_mode = $request->header('App-Mode');
        }
        $master->store_id = $request->input('store_id');
        if($request->input('store_id') > 0) {
            $store = StoreModel::where('id', '=', $request->input('store_id'))->first();
            $master->store_name = $store->name;
        }
        $master->area_id = $request->input('area_id');
        if($request->input('area_id') > 0) {
            $area = AreaModel::where('id', '=', $request->input('area_id'))->first();
            $master->area_name = $area->area;
        }
        $master->nickname = $request->input('nickname');
        $master->position = $request->input('position');
        $master->skills = $request->input('skills');
        $master->avatar = $request->input('avatar');
        $master->intro = $request->input('intro');
        $master->detail = $request->input('detail');
        $master->save();
        return ResultTool::success();
    }

    public function update(Request $request) {
        $master = MasterModel::where('id', '=', $request->input('id'))->first();
        $master->store_id = $request->input('store_id');
        if($request->input('area_id') > 0) {
            $area = AreaModel::where('id', '=', $request->input('area_id'))->first();
            $master->area_name = $area->area;
        }
        $master->area_id = $request->input('area_id');
        $master->nickname = $request->input('nickname');
        $master->position = $request->input('position');
        $master->skills = $request->input('skills');
        $master->avatar = $request->input('avatar');
        $master->intro = $request->input('intro');
        $master->detail = $request->input('detail');
        $master->save();
        return ResultTool::success();
    }

    public function bindUser(Request $request) {
        $user = UserModel::where('mobile', '=', $request->input('mobile'))->first();
        if(is_null($user)) {
            $res = ResultTool::fail();
            $res['errorMsg'] = '手机号非平台用户!';
            return $res;
        }
        $master = MasterModel::where('id', '=', $request->input('master_id'))->first();
        $master->uid = $user->uid;
        $master->save();
        $res = ResultTool::success();
        $res['data']['uid'] = $user->uid;
        return $res;
    }
}